##############################################################################
#
# ReportToLocal.pm
# ================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   Child of Report.pm that facilitates test result reporting
#
# Assumptions:
#   1.) $PAT_REPORT environment variable is set.  For example: 
#	export PAT_REPORT=/root/PerlAutoTest/test/report/
#   2.) $PAT_WEBSVR environment variable is set.  For example:
#	export PAT_WEBSVR=http://172.16.15.1/PAT/
#
# Supported methods:
#   all super class' methods
#   new($_release, $_build);
#   saveTestReport($testScriptName);
#   genSummary();
#
# Note:
#   Variable after ";" is optional variable and value after "||" is 
#   default value
#
##############################################################################

package ReportToLocal;
$VERSION = 1.01;
@ISA =  qw(Report);

use strict;
use AutoTest::Exceptions;

# Constructor: new
# Purpose:     Instantiate a ReportToLocal object
# Parameters:
#       1.) _release : Test image release information
#       2.) _build   : Test image build information
# Returns:    An instance of ReportToLocal class
sub new ($$) {
	if (!defined $ENV{'PAT_REPORT'}) {
		AutoTest::Exception::Environment->throw('PAT_REPORT environment variable not set');
	}
	if (!defined $ENV{'PAT_WEBSVR'}) {
		AutoTest::Exception::Environment->throw('PAT_WEBSVR environment variable not set');
	}
	my $class = shift;
	my $self = { };
	$self = Report->new($_[0], $_[1]);
	bless $self, $class;
	return $self;
}

# Method:	saveTestReport
# Purpose:	Save test result
# Parameters:
#       1.) testScriptName : The name of test script that generated the test result
# Returns:	Void
sub saveTestReport ($) {
	my $self = shift;
	my $testScriptName = shift;
	my $myReleaseString = $self->release();
	my $myBuildString = $self->build();
	my $absoluteBuild = $myReleaseString . "." . $myBuildString;
	my $releaseDir = $ENV{'PAT_REPORT'} . $myReleaseString;
	my $buildDir = $ENV{'PAT_REPORT'} . $myReleaseString . "/" . $myBuildString;

	# Make a release directory if it does not exists already
	if (system("ls", "-l", "$releaseDir") != 0) {
        	system("mkdir", "$releaseDir");
	}
	
	# Make a build directory if it does not exists already
	if (system("ls", "-l", "$buildDir") != 0) {
        	system("mkdir", "$buildDir");
	}
	
	# Make sure there is a proper directory structure
	if (system("ls", "-l", "$buildDir") != 0) {
        	die "ls -l $buildDir failed: $?";
	}
	
	# Remove previous report file
	my $reportFileName = "$buildDir/$testScriptName.testReport.html";
	system("rm", "-f", "$reportFileName");

	# Open the report file for saving test result	
	open(HANDOUT, "> $reportFileName") or die "Can't open $reportFileName: $!\n";
	my $summary = "SUMMARY: Total: " . $self->total() . " Pass: " . $self->pass() . " Fail: " . $self->fail() . " NotRun: " . $self->getNotRun();
	$self->appendTestReport($summary);
	print HANDOUT "<html>";
	print HANDOUT "<PRE>";
	print HANDOUT $self->testReport();
	print HANDOUT "</PRE>";
	print HANDOUT "<b><a href=\"$ENV{'PAT_WEBSVR'}report\/$myReleaseString\/$absoluteBuild.summary.html\"><font color=#007000>$absoluteBuild SUMMARY</font></a></b>";
	print HANDOUT "<br>";
	print HANDOUT "<b><a href=\"$ENV{'PAT_WEBSVR'}log\/$testScriptName.log\"><font color=#007000>Download log file</font></a></b>";
	print HANDOUT "</html>";
	close(HANDOUT);
}
	
# Method:	genSummary
# Purpose:	Generate a summary report of all tests done
#		on specified release and build
# Parameters:	None
# Returns:	Void
sub genSummary () {
        my $self = shift;
	my $myReleaseString = $self->release();
	my $myBuildString = $self->build();
	my $absoluteBuild = $myReleaseString . $myBuildString;
        my $releaseDir = $ENV{'PAT_REPORT'} . $myReleaseString;
        my $buildDir = $ENV{'PAT_REPORT'} . $myReleaseString . "/" . $myBuildString;
	my $fileName = $myReleaseString . "." . $myBuildString . ".summary.html";

	# Initiate possible status cell contain
	my $status = "<td></td>";
	my $red = "<td bgcolor=red> pass != 100% </td>";
	my $green = "<td bgcolor=green> pass == 100% </td>";

	# Make sure there is a build directory
	if (system("ls", "-l", "$buildDir") != 0) {
        	die "ls -l $buildDir  failed: $?";
	}
	
	# Remove summary file if exists
	my $summaryFile = "$releaseDir/$fileName";
	system("rm", "-f", "$summaryFile");

	# Set summary file table header
	open(HANDOUT, "> $summaryFile") or die "Can't open $summaryFile: $!\n";
	print HANDOUT "<HTML><BODY><table border> <tr><td colspan=6>Release: $myReleaseString  Build $myBuildString </td></tr><tr> <td>Status</td><td>Script Name</td><td>Total case</td> <td>Pass</td><td>Fail</td><td>Not run</td> </tr>";
	
	# Open the build directory and get a report file list
	opendir(DIR, "$buildDir");
	my @files = grep /\S+\.testReport/, readdir(DIR);
	closedir(DIR);

	# For each report file, extract summary information and put them in a table	
	foreach my $reportFile (@files) {
        	open(F, "$buildDir/$reportFile") or die "$buildDir/$reportFile does not exist: $!\n";
        	while(my $line = <F>) {
        	if ($line =~ /SUMMARY/) {
                	$line =~ m{Total: +(\d+) +Pass: +(\d+) +Fail: +(\d+) +NotRun: +(\d+)};
                	my $total = defined($1) ? $1 : 0;
                	my $pass = defined($2) ? $2 : 0;
                	my $fail = defined($3) ? $3 : 0;
                	my $notRun = defined($4) ? $4 : 0;
                	if (($total == $pass) && ($pass != 0)) {
                        	$status = $green;
                	} else {
                        	$status = $red;
                	}
                	print HANDOUT "$status <td><a href=\"$ENV{'PAT_WEBSVR'}report\/$myReleaseString\/$myBuildString\/$reportFile\">$reportFile</a></td><td>$total</td><td>$pass</td><td>$fail</td><td>$notRun</td></tr>";
                	last;
                	}
        	}
        	close(F);
	}
	
	# Encode the rest of the table and close file handle
	print HANDOUT "</table></BODY>";
	close(HANDOUT);
}
